package com.kj.platformlogistics.my.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

import com.kj.platformlogistics.R;
import com.kj.platformlogistics.base.BaseActivity;
import com.kj.platformlogistics.constants.UrlConstants;
import com.kj.platformlogistics.http.HttpRequest;
import com.kj.platformlogistics.http.HttpStringCallBack;
import com.kj.platformlogistics.util.GlideEngine;
import com.kj.platformlogistics.util.StringUtils;
import com.kj.platformlogistics.util.ToastUtil;
import com.kj.platformlogistics.widget.PhotoPickBottomDialog;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;

import org.json.JSONException;
import org.json.JSONObject;

public class ActivityPhoneModify extends BaseActivity implements View.OnClickListener {
    private TextView tvSure;
    private EditText etPhone;
    private String phone="";

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, ActivityPhoneModify.class);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tvSure:
                if (phone.equals("")||! StringUtils.isPhoneNumber(phone)){
                    Toast.makeText(ActivityPhoneModify.this,"请正确填写您要修改的手机号",Toast.LENGTH_SHORT).show();
                    return;
                }else {
                    updateData();
                }

                break;
        }
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_phone_modify;
    }

    @Override
    protected void initView() {
        setTitleText("修改手机号");
        tvSure=findViewById(R.id.tvSure);
        etPhone=findViewById(R.id.etPhone);

        tvSure.setOnClickListener(this::onClick);

        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                phone=s.toString().trim();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    @Override
    protected void initData() { }

    /**
     * 获取要使用的标题栏样式(此函数由子类重写)
     * @return
     */
    @Override
    protected int getTitleBarType() {
        return 0;
    }


    public void updateData(){
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("phone",phone);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new HttpRequest(ActivityPhoneModify.this).doPost(UrlConstants.logistics_modifyStationUserInfo, "", jsonObject,new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success = jsonObject.getBoolean("success");
                    if (success) {
                        ToastUtil toastUtil = new ToastUtil(getContext(), R.layout.popu_success1, "修改手机号成功！", 1);
                        toastUtil.show();
                        finish();
                    }else {
                        ToastUtil toastUtil = new ToastUtil(getContext(), R.layout.popu_success1, message, 1);
                        toastUtil.show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) { }
        });
    }
}